<template>
  <div class="app-container">
    <!-- 搜索和操作栏 -->
    <el-row :gutter="20" class="mb10" style="margin: 10px 0;">
      <el-col :span="18" class="text-right">
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button type="success" @click="handleAdd">新增分类</el-button>
      </el-col>
    </el-row>

    <!-- 数据表格 -->
    <el-table size="mini" :data="tableData" border>
      <el-table-column type="index" />
      <el-table-column prop="categoryName" label="分类名称" width="180"/>
      <el-table-column prop="description" label="分类描述"/>
      <!-- 新增分类图片列 -->
      <el-table-column label="分类图片" width="180" align="center">
        <template slot-scope="scope">
          <el-image 
            style="width: 100px; height: 60px"
            :src="scope.row.imgUrl"
            :preview-src-list="[scope.row.imgUrl]"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column prop="sn" label="排序号" width="120" align="center"/>
      <el-table-column label="是否热门" width="120" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isHot ? 'success' : 'info'">
            {{ scope.row.isHot ? '热门' : '普通' }}
          </el-tag>
        </template>
      </el-table-column>
      <!-- 新增状态列 -->
      <el-table-column label="状态" width="120" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.delFlag === 0 ? 'danger' : 'success'">
            {{ scope.row.delFlag === 0 ? '禁用' : '启用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button size="mini" plain type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑弹窗 -->
    <el-dialog 
      :title="(formData.id ? '编辑' : '新增') + '分类'" 
      :visible.sync="dialogVisible"
      width="600px"
    >
      <el-form ref="form" :model="formData" label-width="100px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="formData.categoryName" placeholder="请输入分类名称"/>
        </el-form-item>
        
        <el-form-item label="分类描述" prop="description">
          <el-input 
            v-model="formData.description" 
            type="textarea" 
            :rows="3"
            placeholder="请输入分类描述"
          />
        </el-form-item>

        <el-form-item label="排序号" prop="sn">
          <el-input-number 
            v-model="formData.sn" 
            :min="1" 
            controls-position="right"
          />
        </el-form-item>

        <el-form-item label="是否热门">
          <el-switch
            v-model="formData.isHot"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>

        <el-form-item label="是否启用">
          <el-switch
            v-model="formData.delFlag"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getNewsCategoryList, saveNewsCategory } from '@/api/index'
export default {
  name: 'NewsCategoryManage',
  data() {
    return {
      searchParams: {
        isHot: ''
      },
      tableData: [],
      pagination: {
        current: 1,
        size: 999,
        total: 0
      },
      dialogVisible: false,
      formData: {
        id: null,
        categoryName: '',
        description: '',
        imgUrl:'',
        sn: 1,
        isHot: 0
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    // 获取分类数据
    async fetchData() {
      getNewsCategoryList(this.searchParams).then(res => {
        this.tableData = res
      })
    },
    
    // 打开新增弹窗
    handleAdd() {
      this.formData = {
        id: null,
        categoryName: '',
        description: '',
        imgUrl:'',
        sn: 1,
        isHot: 0
      }
      this.dialogVisible = true
    },
    
    // 打开编辑弹窗
    handleEdit(row) {
      this.formData = { ...row }
      this.dialogVisible = true
    },
    
    // 提交表单
    submitForm() {
      saveNewsCategory(this.formData).then(res => {
        this.$message.success('添加成功')
        this.dialogVisible = false
        this.fetchData()
      })
    },
    
    // 删除分类
    handleDelete(id) {
      this.$confirm('确认删除该分类吗？', '提示', {
        type: 'warning'
      }).then(async () => {
        // 这里调用实际删除接口
        // console.log('删除ID:', id)
        this.fetchData()
      })
    },
    
    // 分页相关方法
    handleSizeChange(val) {
      this.pagination.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pagination.current = val
      this.fetchData()
    },
    
    // 搜索
    handleSearch() {
      this.pagination.current = 1
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.el-tag {
  margin: 2px;
}
</style>